<?php

declare(strict_types=1);

namespace App\CustHub\Mapper;

use App\Common\Constants\StatusConstants;
use App\CustHub\Model\ChOpScheme;
use Hyperf\Cache\Annotation\Cacheable;
use Hyperf\Config\Annotation\Value;
use Hyperf\Database\Model\Builder;
use Hyperf\Database\Model\Model;
use Mine\Abstracts\AbstractMapper;

class ChOpSchemeMapper extends AbstractMapper
{
    #[Value('cache.default.prefix')]
    protected string $prefix;

    public function assignModel()
    {
        $this->model = ChOpScheme::class;
    }

    /**
     * 获取单个配置方案
     * @param mixed $id
     * @return ChOpScheme|Model|string|Builder|null
     */
    #[Cacheable(prefix: 'ch_op_scheme', value: 'id_#{id}', ttl: 15 * 60)]
    public function findById(mixed $id): ChOpScheme|Model|string|Builder|null
    {
        return $this->model::query()->where('id',$id)->first();
    }

    public function deleteCache(mixed $id): void
    {
        $keys[] = $this->prefix . 'ch_op_scheme:id_'.$id;
        redis()->del($keys);
    }
}